//代理
const axios = require('axios');
let origin = 'http://10.220.110.126:8081'
module.exports = async (req,next) => {
    // console.log(req)
    let f = (/app/).test(req.path);
    if (!f) {
        return next();
    }
    let proxy = axios.create({
        baseURL: origin,
        withCredentials: true,
        headers: {
            origin,
            'content-type': 'text/plain;charset=UTF-8',
            referer: origin,
            'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; PRO 6 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043221 Safari/537.36 V1_AND_SQ_7.0.0_676_YYB_D QQ/7.0.0.3135 NetType/WIFI WebP/0.3.0 Pixel/1080'
        },
        transformRequest: [(data) => {
            return JSON.stringify(data)
        }],
    })
    await proxy.request({
        url:origin+req.path,
        method:req.method,
        data:req.request.body,
        params:req.query
    })
    .then(function(data) {
        // console.log(JSON.stringify(data.data))
        req.body=JSON.stringify(data.data);
    })
    // req.proxy.request(null, function (err, response, body) {
    //     if (!!err) {
    //         next(err)
    //     } else {
    //         for (var key in response.headers) {
    //             res.set(key, response.headers[key])
    //         }
    //         try {
    //             body = JSON.parse(body)
    //         } catch (e) {
    //             console.error(e);
    //         }
    //         res.send(body);
    //     }
    // });
}